package cn.tedu.sp04.order.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.web.util.JsonResult;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@Slf4j
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        log.info("get order,id="+orderId);
        Order order = orderService.getOrder(orderId);
        return JsonResult.ok(order);
    }

    @GetMapping("/")
    public JsonResult addOrder(){
        //模拟post提交数据
        Order order = new Order();
        order.setId("123");
        // 真实项目中, 应该获取已登录的用户数据
        order.setUser(new User(7,null,null));
        order.setItems(Arrays.asList(new Item[]{
                new Item(1,"aaa",1),
                new Item(2,"bbb",2),
                new Item(3,"ccc",3),
                new Item(4,"ddd",4),
                new Item(5,"www",5),
        }));
        orderService.addOrder(order);
        return JsonResult.ok().msg("添加订单成功");
    }

    //用一个空方法过滤掉浏览器自动发起的对网站图标的请求
    //避免影响测试
    @GetMapping("/favicon.ico")
    public void ico(){
        //空方法
    }
}
